package com.example.x.dnuifood_929dx;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.x.dnuifood_929dx.adapter.ShopAdapter;
import com.example.x.dnuifood_929dx.bean.ShopBean;
import com.example.x.dnuifood_929dx.fragment.CollectFragment;
import com.example.x.dnuifood_929dx.fragment.HomeFragment;
import com.example.x.dnuifood_929dx.fragment.MeFragment;
import com.example.x.dnuifood_929dx.fragment.SearchFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by X on 2018/11/15.
 */

public class MainActivity   extends AppCompatActivity {
    private ImageButton ib1,ib2,ib3,ib4;
    private TextView tv1,tv2,tv3,tv4;
    private ViewPager vp1;
    private List<Fragment> listfragment;
    private FragmentPagerAdapter fpadapter;
    private RecyclerView recyclerView;
    private List<ShopBean> shopBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ib1=(ImageButton)findViewById(R.id.ib1);
        ib2=(ImageButton)findViewById(R.id.ib2);
        ib3=(ImageButton)findViewById(R.id.ib3);
        ib4=(ImageButton)findViewById(R.id.ib4);
        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);
        tv3 = findViewById(R.id.tv3);
        tv4 = findViewById(R.id.tv4);
/*        gp1 = findViewById(R.id.group1);
        gp2 = findViewById(R.id.group2);
        gp3 = findViewById(R.id.group3);*/

        vp1=(ViewPager)findViewById(R.id.vp1);
        initEvents();
        initFragment();

        ShopAdapter mAdapter = new ShopAdapter(this,listfragment);

//        recyclerView = findViewById(R.id.rv01);
//        recyclerView.setLayoutManager(new LinearLayoutManager(this));//?
//        recyclerView.setItemAnimator(new DefaultItemAnimator());
//        shopBeans = getData();
//        recyclerView.setAdapter(new ShopAdapter(this,shopBeans));

    }
    private void resetImage()
    {
        ib1.setImageResource(R.drawable.sy);
        ib2.setImageResource(R.drawable.sc);
        ib3.setImageResource(R.drawable.ss);
        ib4.setImageResource(R.drawable.wd);
//        ib1.setImageResource(android.R.drawable.btn_star_big_off);
//        ib2.setImageResource(android.R.drawable.btn_star_big_off);
//        ib3.setImageResource(android.R.drawable.btn_star_big_off);
//        ib4.setImageResource(android.R.drawable.btn_star_big_off);

    }
    private void selectTab(int i)
    { switch (i)
    {
        case 0:ib1.setImageResource(R.drawable.sy);
            break;
        case 1:ib2.setImageResource(R.drawable.sc);
            break;
        case 2:ib3.setImageResource(R.drawable.ss);
            break;
        case 3:ib4.setImageResource(R.drawable.wd);
            break;

    }
        vp1.setCurrentItem(i);
    }

    View.OnClickListener onClickListener =new View.OnClickListener() {
        @Override
        public void onClick(View view) {
//先将四个按钮都设成未选图片
            resetImage();
            switch (view.getId())
            {
                case R.id.ib1:
                case R.id.tv1:
                    selectTab(0); break;
                case R.id.ib2:
                case R.id.tv2:
                    selectTab(1); break;

                case R.id.tv3:
                case R.id.ib3:
                    selectTab(2); break;

                case R.id.tv4:
                case R.id.ib4:
                    selectTab(3); break;
            }
        }
    };
    private void initEvents() {
        ib1.setOnClickListener(onClickListener);
        ib2.setOnClickListener(onClickListener);
        ib3.setOnClickListener(onClickListener);
        ib3.setOnClickListener(onClickListener);
        tv1.setOnClickListener(onClickListener);
        tv2.setOnClickListener(onClickListener);
        tv3.setOnClickListener(onClickListener);
    }
    private void initFragment() {
        listfragment=new ArrayList<Fragment>();
        listfragment.add(new HomeFragment());
        listfragment.add(new CollectFragment());
        listfragment.add(new SearchFragment());
        listfragment.add(new MeFragment());
        fpadapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) { return listfragment.get(position);
            }
            @Override
            public int getCount() { return listfragment.size();}
        };
        vp1.setAdapter(fpadapter);
        vp1.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int
                    positionOffsetPixels) {}
            @Override
            public void onPageSelected(int position) {
                vp1.setCurrentItem(position);
                resetImage();
                selectTab(position);
            }
            @Override
            public void onPageScrollStateChanged(int state) {}
        });
    }
    private List<ShopBean> getData() {
        List<ShopBean> list = new ArrayList() ;
        ShopBean shopBean = new ShopBean();
        shopBean.setShopname("满口川湘菜");
        shopBean.setAddress("东软三期食堂");
        shopBean.setPic(R.drawable.ic_launcher_background+"");
        shopBean.setLevel(5);
        list.add(shopBean);

        shopBean = new ShopBean();
        shopBean.setShopname("东北一家人");
        shopBean.setAddress("东软三期食堂");
        shopBean.setPic(R.drawable.ic_launcher_background+"");
        shopBean.setLevel(2);
        list.add(shopBean);

        shopBean = new ShopBean();
        shopBean.setShopname("姐妹麻辣烫");
        shopBean.setAddress("东软三期食堂");
        shopBean.setPic(R.drawable.ic_launcher_background+"");
        shopBean.setLevel(3);
        list.add(shopBean);

        shopBean = new ShopBean();
        shopBean.setShopname("西湖印象");
        shopBean.setAddress("东软三期食堂");
        shopBean.setPic(R.drawable.ic_launcher_background+"");
        shopBean.setLevel(4);
        list.add(shopBean);

        shopBean = new ShopBean();
        shopBean.setShopname("王子普");
        shopBean.setAddress("东软三期食堂");
        shopBean.setPic(R.drawable.ic_launcher_background+"");
        shopBean.setLevel(2);
        list.add(shopBean);

        return list;
    }
}